package com.qinyeit.codegen;

import com.qinyeit.codegen.entity.PropConfig;
import com.qinyeit.codegen.entity.Table;
import jetbrick.template.JetEngine;
import jetbrick.template.JetTemplate;
import jetbrick.util.DateUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 根据模板生成项目中所用到实体业务等等
 * ClassName: GenerateProject <br/>
 * Function: 模版生成代码 <br/>
 * date: 2015年8月11日 上午9:07:20 <br/>
 *
 * @author liyang
 * @since JDK 1.7
 */
public class GenerateProject {
    private Logger log=Logger.getLogger(GenerateProject.class);
    private static JetEngine engine = null;
    private Table table = null;
    private PropConfig config = null;


    public GenerateProject(Table table, PropConfig propConfig) {
        this.table = table;
        this.config = propConfig;
    }

    /**
     * 初始化模版引擎
     * getEngine:(如果模版引擎存在就不在创建). <br/>
     *
     * @since JDK 1.7
     */
    private static JetEngine getEngine() {
        if (engine == null) {
            engine = JetEngine.create();
        }
        return engine;
    }

    public void genEntity() {
        gen("entity", config.getEntityPath() +
                table.getClassName() + ".java");
    }

    public void genRepo() {
        gen("repository", config.getRepositoryPath() +
                table.getClassName() + "Repository.java");
    }

    public void genService() {
        gen("service", config.getServicePath() +
                table.getClassName() + "Service.java");
    }

    public void genServiceImpl() {
        gen("serviceImpl", config.getServiceImplPath() +
                table.getClassName() + "ServiceImpl.java");
    }

    public void genController() {
        gen("controller", config.getControllerPath() +
                table.getClassName() + "sController.java");
    }

    public void genServiceTest() {
        gen("serviceTest", config.getServiceTestPath()
                + table.getClassName() + "ServiceTest.java");
    }
    public void genControllerTest(){
        gen("controllerTest", config.getControllerTestPath()
                + table.getClassName() + "ControllerTest.java");
        gen("controllerTestConfig", config.getControllerTestConfigPath()
                + table.getClassName() + "ControllerTestConfiguration.java");
    }
    public void genVue() {
        String vuePath =config.getVuePath();
        String vueRouterPath=config.getVueRouterPath();
        String vueView=vuePath+table.getHumpName()+"s/";
        gen("vue/dataTable.vue",vueView+"dataTable.vue");
        gen("vue/index.vue",vueView+"index.vue");
        gen("vue/show.vue",vueView+"show.vue");
        if(config.isModal()){
            gen("vue/model.vue",vueView+"model.vue");
            gen("vue/model.router",vueRouterPath+table.getHumpName()+".router");
        }else{
            gen("vue/add.vue",vueView+"add.vue");
            gen("vue/update.vue",vueView+"update.vue");
            gen("vue/table.router",vueRouterPath+table.getHumpName()+".router");
        }


    }

    /**
     * 根据模版生成文件
     * gen:(这里用一句话描述这个方法的作用). <br/>
     *
     * @param template 模板文件名称
     * @param savePath 保存路径
     * @since JDK 1.7
     */
    public void gen(String template, String savePath) {

        try {
            JetEngine engine = getEngine();
            JetTemplate tmp = engine.getTemplate("template/" + template + ".btl");
            Map<String, Object> map = new HashMap<>();
            if(config==null){
                return ;
            }
            map.put("config", config);
            map.put("curDate", DateUtils.format("yyyy-MM-dd HH:mm:ss"));
            map.put("table", table);
            map.put("modal", config.isModal());
            map.put("tree", config.isTree());

            File file = new File(savePath);
            if(!file.exists()){
                File parentFile = file.getParentFile();

                if(!parentFile.exists() && !parentFile.mkdirs()){
                    return;
                }
                if(!file.createNewFile()){
                    return ;
                }
            }
            OutputStream os = new FileOutputStream(file);
            tmp.render(map, os);
            os.flush();
            os.close();
            log.info("生成文件："+file.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




}
